package ro.msg.quiz.view;

import java.io.DataInputStream;
import java.util.ArrayList;

import ro.msg.quiz.Connection.ConnectJdbc;
import ro.msg.quiz.Model.QuizJdbcModel;
import ro.msg.quiz.controller.QuizController;
import ro.msg.quiz.repository.MultipleChoiceQ;
import ro.msg.quiz.repository.Question;
import ro.msg.quiz.repository.Quiz;
import ro.msg.quiz.repository.SingleChoiceQ;
import ro.msg.quiz.repository.TextQ;

/**
 * @author Blaj
 * 
 */
public class NoGUIview {
	private QuizController ctrl;
	ConnectJdbc conn = new ConnectJdbc();
	QuizJdbcModel dbc = new QuizJdbcModel();

	/**
	 * @param ctrl
	 */
	public NoGUIview(QuizController ctrl) {
		this.ctrl = ctrl;
	}

	/**
	 * add new quiz
	 * 
	 */
	@SuppressWarnings("deprecation")
	public void addNewQuiz() {

		String name, id;
		int id_final;
		DataInputStream d = new DataInputStream(System.in);

		try {
			System.out.println("The id quiz is:");
			id = d.readLine();
			System.out.println("The name of the quiz is:");
			name = d.readLine();
			id_final = Integer.parseInt(id);
			Quiz s = new Quiz(id_final, name);
			this.ctrl.addQuiz(s);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	/**
	 * Add new multiple question
	 */
	@SuppressWarnings("deprecation")
	public void addNewMultipleChoiceQuestion() {

		String name, opt, nr, id;
		int nr_final, id_final;
		Integer opt_final;
		DataInputStream d = new DataInputStream(System.in);
		ArrayList<String> options = new ArrayList<String>();
		ArrayList<Integer> correctOptions = new ArrayList<Integer>();
		try {
			System.out.println("The id of the question is:");
			id = d.readLine();
			id_final = Integer.parseInt(id);

			System.out.println("The text of the question is:");
			name = d.readLine();

			System.out.println("The number of options is:");
			nr = d.readLine();
			nr_final = Integer.parseInt(nr);
			int noofoptions = nr_final;
			for (int i = 1; i <= nr_final; i++) {
				System.out.println("The " + i + "'st option is:");
				options.add(d.readLine());
			}
			System.out.println("The number of correct options is:");
			nr = d.readLine();
			nr_final = Integer.parseInt(nr);
			for (int i = 1; i <= nr_final; i++) {
				System.out.println("The " + i + "'st correct option is");
				opt = d.readLine();
				opt_final = Integer.valueOf(opt);
				correctOptions.add(opt_final);
			}
			Question s = new MultipleChoiceQ(id_final, name, noofoptions,
					options, correctOptions);
			ctrl.addQuestion(s);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	/**
	 * Delete question
	 * 
	 */
	@SuppressWarnings("deprecation")
	public void DeleteQuestion() {
		String x;
		int x_final;
		DataInputStream d = new DataInputStream(System.in);

		try {
			System.out.println("Id of the question is :");
			x = d.readLine();
			x_final = Integer.parseInt(x);
			this.ctrl.removeQuestion(x_final);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	/**
	 * Delete quiz
	 */
	@SuppressWarnings("deprecation")
	public void DeleteQuiz() {
		String x;
		int x_final;
		DataInputStream d = new DataInputStream(System.in);

		try {
			System.out.println("Id of the quiz is :");
			x = d.readLine();
			x_final = Integer.parseInt(x);
			this.ctrl.removeQuiz(x_final);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	/**
	 * Bind a question to a quiz
	 */
	@SuppressWarnings("deprecation")
	public void AddQuestionToQuiz() {
		String quiz_string, question_string;
		int quiz_int, question_int;
		DataInputStream d = new DataInputStream(System.in);

		try {
			System.out.println("Id of the quiz is :");
			quiz_string = d.readLine();
			quiz_int = Integer.parseInt(quiz_string);
			System.out.println("Id of the question is :");
			question_string = d.readLine();
			question_int = Integer.parseInt(question_string);
			this.ctrl.addQuestionToQuiz(quiz_int, question_int);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	/**
	 * Remove a question from quiz
	 */
	@SuppressWarnings("deprecation")
	public void RemoveQuestionFromQuiz() {
		String quiz_string, question_string;
		int quiz_int, question_int;
		DataInputStream d = new DataInputStream(System.in);

		try {
			System.out.println("Id of the quiz is :");
			quiz_string = d.readLine();
			quiz_int = Integer.parseInt(quiz_string);
			System.out.println("Id of the question is :");
			question_string = d.readLine();
			question_int = Integer.parseInt(question_string);
			this.ctrl.removeQuestionFromQuiz(quiz_int, question_int);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	/**
	 * Print Map
	 */
	@SuppressWarnings("deprecation")
	public void printMap() {
		this.ctrl.printMap();
	}

	/**
	 * Adds new single choice question
	 */
	@SuppressWarnings("deprecation")
	public void addNewSingleChoiceQuestion() {

		String name, opt, nr, id;
		int nr_final, id_final;
		Integer opt_final;
		DataInputStream d = new DataInputStream(System.in);
		ArrayList<String> options = new ArrayList<String>();
		ArrayList<Integer> correctOptions = new ArrayList<Integer>();
		try {
			System.out.println("The id of the question is:");
			id = d.readLine();
			id_final = Integer.parseInt(id);

			System.out.println("The text of the question is:");
			name = d.readLine();

			System.out.println("The number of options is:");
			nr = d.readLine();
			nr_final = Integer.parseInt(nr);
			int noofoptions = nr_final;
			for (int i = 1; i <= nr_final; i++) {
				System.out.println("The " + i + "'st option is:");
				options.add(d.readLine());
			}
			System.out.println("The correct option is:");
			nr = d.readLine();
			nr_final = Integer.parseInt(nr);
			Question s = new SingleChoiceQ(id_final, name, options, nr_final);
			ctrl.addQuestion(s);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	/**
	 * Adds a new text question
	 * 
	 */
	public void addNewTextQuestion() {

		String name, answer, id;
		int nr_final, id_final;
		Integer opt_final;
		DataInputStream d = new DataInputStream(System.in);
		ArrayList<String> options = new ArrayList<String>();
		ArrayList<Integer> correctOptions = new ArrayList<Integer>();
		try {
			System.out.println("The id of the question is:");
			id = d.readLine();
			id_final = Integer.parseInt(id);

			System.out.println("The text of the question is:");
			name = d.readLine();

			System.out.println("The answer is:");
			answer = d.readLine();
			Question s = new TextQ(id_final, name, answer);
			ctrl.addQuestion(s);
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	/**
	 * Shows all questions registered
	 */
	public void showAllQuestions() {
		this.ctrl.printAllQuestions();
	}

	/**
	 * Show all quizzes registered
	 * 
	 */
	public void showAllQuizes() {
		this.ctrl.printAllQuizes();
	}

	/**
	 * Adds a new question here the three derived methods of addQuestion are
	 * called
	 */
	public void addNewQuestion() {
		String ss;
		int com = 0;
		System.out.println("1.Add new Text Question");
		System.out.println("2.Add new Single Choice Question");
		System.out.println("3.Add new Multiple Choice Question");

		DataInputStream d = new DataInputStream(System.in);
		try {
			ss = d.readLine();
			com = Integer.parseInt(ss);
		} catch (Exception e) {
			System.out.println(e);
		}
		switch (com) {
		case 1:
			this.addNewTextQuestion();
			break;
		case 2:
			this.addNewSingleChoiceQuestion();
			;
			break;
		case 3:
			this.addNewMultipleChoiceQuestion();
			break;
		default:
			System.out.println("Invalid command");
			break;
		}
	}

	/**
	 * Show ui method
	 */
	@SuppressWarnings("deprecation")
	public void showUI() {
		while (true) {
			String ss;
			int com = 0;
			System.out.println("1.Add new question");
			System.out.println("2.Show all questions registered");
			System.out.println("3.Add new quiz");
			System.out.println("4.Show all quizes");
			System.out.println("5.Delete Quiz");
			System.out.println("6.Delete Question");
			System.out.println("7.Add Question to Quiz");
			System.out.println("8.Print map");
			System.out.println("9.Remove question from quiz");
			System.out.println("10.Import sql database");
			System.out.println("11.Export sql database");
			DataInputStream d = new DataInputStream(System.in);
			try {
				ss = d.readLine();
				com = Integer.parseInt(ss);
			} catch (Exception e) {
				System.out.println(e);
			}
			switch (com) {
			case 1:
				this.addNewQuestion();
				break;
			case 2:
				this.showAllQuestions();
				break;
			case 3:
				this.addNewQuiz();
				break;
			case 4:
				this.showAllQuizes();
				break;
			case 5:
				this.DeleteQuiz();
				break;
			case 6:
				this.DeleteQuestion();
				break;
			case 7:
				this.AddQuestionToQuiz();
				break;
			case 8:
				this.printMap();
				break;
			case 9:
				this.RemoveQuestionFromQuiz();
				break;
			case 10:
				this.ctrl.setModel(this.dbc.ImportSqlDatabase());
				break;
			case 11:
				this.dbc.ExportSqlDatabase(this.ctrl.getModel());
				break;
			default:
				System.out.println("Invalid command");
				break;
			}
		}
	}

}
